// 设置动作与ui的连接函数设置–connect.js
import {ACTION_LOGIN, SiderCollpased, SiderOnOpenChange, AddTabs} from "./actionType";
import {receive, siderMenuList, templateAction, toggleCollapsed, menuOnOpenChange, addTabs} from "./actionCreator";
import http from "@/api/http";



export const mapLogin = {
    // 把store中的数据映射到这个组件变成props，所以如果要取store中的数据就需要通过this.props.参数
    // 每当store state发生变化时，就被调用。接收整个store state，并且返回一个该组件所需要的数据对象
    mapStateToProps (state) {
        return state.getLogin;
    },
    //mapDispatchToProps可以接收 dispatch() 方法并返回期望注入到展示组件的 props 中的回调方法。
    mapDispatchToProps (dispatch) {
        return {
            handleLogin: (data) => {
                return fetchPosts('/login.do', ACTION_LOGIN, 'loginData', data)(dispatch)
            }
        }
    }
}











function fetchPosts(url, actionType, subreddit, data) {
    return dispatch => {
        dispatch(receive(actionType, subreddit, '暂无数据')) //切换数据
        return http.post(url, data)
            .then(res => {
                dispatch(receive(actionType, subreddit, res))
            }).catch(err => {
                console.log(err)
            });
    }
}


//退出
export const maploginout = {
    mapStateToProps(state) {
        return { headerData: {}, ...state.slidecollapsed}
    },
    mapDispatchToProps(dispatch) {
        return {
            onLogout: (data) => {
                // return fetchPosts('/logout.do', action_slidecollapsed.type, 'logoutData', data)(dispatch)
                return dispatch()
            }
        }
    }

}


/**
 * 首页sider数据获取、展开回收、只展开当前父级菜单
 * @type {{mapStateToProps(*): *, mapDispatchToProps(*, *): *}}
 */
export const mapIndexSider = {
    mapStateToProps(state) {
        return {...state.sider, ...state.tabs}
    },
    mapDispatchToProps(dispatch, ownProps) {
        console.log("----------------------connect-------------------------");
        return {
            toggleCollapsed: () => {
                // dispatch将action传递给store
                dispatch(toggleCollapsed);
            },
            getSiderData: () => {
                // 在这里请求menu数据
                const list = [{key : "sub1", type: "", title: "基础信息", routes: [
                        {key : "sub1_1", type: "", title: "基础信息"},
                        {key : "sub1_2", type: "", title: "基础信息"},
                        {key : "sub1_3", type: "", title: "基础信息"},
                        {key : "sub1-4", type: "", title: "基础信息"},
                        {key : "sub1—5", type: "", title: "基础信息"},
                    ]},
                    {key : "sub2", type: "", title: "采购管理", routes: [
                            {key : "sub2_1", type: "", title: "基础信息"},
                            {key : "sub2_2", type: "", title: "基础信息"},
                            {key : "sub2_3", type: "", title: "基础信息"},
                            {key : "sub2-4", type: "", title: "基础信息"},
                            {key : "sub2—5", type: "", title: "基础信息"},
                        ]},
                    {key : "sub3", type: "", title: "销售管理", routes: [
                            {key : "sub3_1", type: "", title: "基础信息"},
                            {key : "sub3_2", type: "", title: "基础信息"},
                            {key : "sub3_3", type: "", title: "基础信息"},
                            {key : "sub3-4", type: "", title: "基础信息"},
                            {key : "sub3—5", type: "", title: "基础信息"},
                        ]}];
                const rootKeys = list.map(item => {
                   return item.key
                });
                dispatch(siderMenuList(list, rootKeys));
            },
            onOpenChange: (openKey) => {
                dispatch(menuOnOpenChange(openKey));
            },
            onClick: (item) => {
                dispatch(addTabs(item));
            }
        }
    }
}
